<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	class jiggy extends Plugins{
		private static $patterns = array(
			"/Uitloggen/i",
			"/Hallo (.*?), je saldo is: <strong>([0-9.]*) Jiggies<\/strong>/i",
			"/E-mail Inbox<\/h1>(.*?)<\/table>/si",
			"/new Jiggy.Emails\(\"([0-9]*)\"\)/i", // Jiggy UserID
			"/<a class=\"jiggy-email-link-open\" href=\"#([0-9]*)\">(.*?)<\/a>/i", // Email ID and Title
			"/track.php\?uid=([0-9]*)&pid=([0-9]*)&h=&sid=([0-9]*)/i",
			"/name=\"a\" src=\"(.*?)\"/i",
			"/var URL        = \"(.*?)\"/i",
			"/&euro; ([0-9,]*)<\/p>/i"
		);
		private static $plugin_name = "Jiggy";
		private static $cookie_name = "jiggy.cookie";
		private static $cert_name = "jiggy.crt";
		function init(){
			message(self::$plugin_name . " loaded","COMMENT");
			return true;
		}
		function login(){
			$postfields = "log=".JIGGY_USER."&pwd=".JIGGY_PASS."&redirect_to=http://www.jiggy.nl/member";
			$curl_options = array(
				CURLOPT_POST 		=> true,
				CURLOPT_POSTFIELDS	=> $postfields,
				CURLOPT_COOKIEJAR	=> DATA_PATH.self::$cookie_name, // Write the cookie
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$login = self::getPage("http://www.jiggy.nl/login?action=login", $curl_options);
			if(self::Parse(self::$patterns[0], $login, 0) == "Uitloggen"){
				message("Succesfully signed in as ".self::Parse(self::$patterns[1], $login, 1).".","NOTICE");
				message("Jiggies: ".self::Parse(self::$patterns[1], $login, 2),"NOTICE");
				message("Euro: ".self::Parse(self::$patterns[8], $login, 1),"NOTICE");
			}
		}
		function emails_lezen(){
			$counter = 0;
			message("Emails lezen", "COMMENT");
			$trigger = false;
			$curl_options = array(
				CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name,
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$content = self::getPage("http://www.jiggy.nl/member/emails",$curl_options);
			$userid = self::Parse(self::$patterns[3],$content,1);
			$unreaded_mails = self::Parse(self::$patterns[2],$content,1);
			$links = explode("<td>",$unreaded_mails);
			if(count($links)>1){
				foreach($links as $l){
					$mail_id = self::Parse(self::$patterns[4],$l,1);
					$mail_title = self::Parse(self::$patterns[4],$l,2);
					if($mail_id != ""){
						$curl_options = array(
							CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name, // Read the cookie
						);
						$content = self::getPage("http://www.jiggy.nl/ajax/user/".$userid."/inbox/preview/".$mail_id."?add_auto_login=false",$curl_options);
						$hash_stuff = self::Parse(self::$patterns[5],$content,0);
						$final_link = "http://www.jiggy.nl/".$hash_stuff;
						$curl_options = array(
							CURLOPT_FOLLOWLOCATION	=> true,
							CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name,
							CURLOPT_SSL_VERIFYHOST => 0,
							CURLOPT_SSL_VERIFYPEER => 0
						);
						$content = self::getPage($final_link,$curl_options);
						
						/*$var1 = self::Parse(self::$patterns[6],$content,1);
						$content1 = self::getPage($var1,$curl_options);
						
						$var2 = self::Parse(self::$patterns[7],$content1,1);
						$content2 = self::getPage($var2,$curl_options); // This normally goes automaticly by Javascript.*/
						
						$counter++;
						message($counter." Read email: ".$mail_id." ".$mail_title,"COMMENT");
						sleep(15);
						$trigger = true;
					}
				}
			}
			if($trigger == false){
				// No new mails
				message("No new emails.", "NOTICE");
			}
		}
		function action(){
			$curl_options = array(
				CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name,
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			); // Read the cookie
			$login = self::getPage("http://www.jiggy.nl/member", $curl_options);
			if(self::Parse(self::$patterns[0], $login, 0) != "Uitloggen"){
				message("Signed out!","ERROR");
				self::login();
			}
			else if(self::Parse(self::$patterns[0], $login, 0) == "Uitloggen"){
				message("Succesfully signed in as ".self::Parse(self::$patterns[1], $login, 1).".","NOTICE");
				message("Jiggies: ".self::Parse(self::$patterns[1], $login, 2),"NOTICE");
				message("Euro: ".self::Parse(self::$patterns[8], $login, 1),"NOTICE");
			}
			self::emails_lezen();
			return true;
		}
	}
	
?>
